package com.sjm.suchbear.easymap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sjm.suchbear.easymap.Util.SP;
import com.sjm.suchbear.easymap.Util.T;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WelcomeActivity extends Activity {

    @BindView(R.id.tv_searchMap)
    TextView tvSearchMap;
    @BindView(R.id.tv_userLogin)
    TextView tvUserLogin;
    @BindView(R.id.tv_userGo)
    TextView tvUserGo;
    @BindView(R.id.tv_userLogOut)
    TextView tvUserLogOut;

    private boolean isLogin = false;
    final private static int LOGINCODE = 0x1;
    final private static int SUCCESS = 0;
    final private static int FAILD = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initview();
    }

    private void initview() {
        isLogin = !SP.getUid(getBaseContext()).equals("");
        if (isLogin) {
            tvUserGo.setVisibility(View.VISIBLE);
            tvUserLogOut.setVisibility(View.VISIBLE);
            tvUserLogin.setVisibility(View.GONE);
        } else {
            tvUserLogin.setVisibility(View.VISIBLE);
            tvUserGo.setVisibility(View.GONE);
            tvUserLogOut.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.tv_searchMap, R.id.tv_userLogin, R.id.tv_userGo, R.id.tv_userLogOut})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tv_searchMap:
                intent = new Intent(WelcomeActivity.this, MainActivity.class);
                intent.putExtra("model", 0); //仅搜索
                startActivity(intent);
                break;
            case R.id.tv_userLogin:
                intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                startActivityForResult(intent, LOGINCODE);
                break;
            case R.id.tv_userGo:
                intent = new Intent(WelcomeActivity.this, MainActivity.class);
                intent.putExtra("model", 3); //全部功能
                startActivity(intent);
                break;
            case R.id.tv_userLogOut:
                SP.setLoginUser(getBaseContext(), "", "", "");
                T.showShort(getBaseContext(), "退出成功");
                initview();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LOGINCODE) {
            switch (resultCode) {
                case SUCCESS: //登录成功
                    initview();
                    break;
                case FAILD: //登录失败
                    initview();
                    break;
            }
        }
    }
}
